package com.woniuxy.microfundservice.service.impl;

import com.woniuxy.bean.ResultBean;
import com.woniuxy.microfundservice.bean.FundMsg;
import com.woniuxy.microfundservice.bean.FundNavigation;
import com.woniuxy.microfundservice.bean.info.FundFullInfo;
import com.woniuxy.microfundservice.bean.info.FundGrades;
import com.woniuxy.microfundservice.bean.info.FundManagers;
import com.woniuxy.microfundservice.bean.info.FundPosition;
import com.woniuxy.microfundservice.dao.*;
import com.woniuxy.microfundservice.service.FundFullInfoService;
import com.woniuxy.microfundservice.service.FundPositionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * (FundFullInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-04-07 12:00:40
 */
@Service
@Slf4j
public class FundFullInfoServiceImpl implements FundFullInfoService {
    @Resource
    private FundFullInfoDao fundFullInfoDao;
    @Resource
    private FundNavigationDao fundNavigationDao;
    @Resource
    private FundManagersDao fundManagersDao;
    @Resource
    private FundGradesDao fundGradesDao;
    @Resource
    private FundPositionDao fundPositionDao;
    @Resource
    private FundPositionService fundPositionService;
    @Resource
    private AppCodeDao appCodeDao;

    @Resource
    private RestTemplate restTemplate;

    @Override
    public int addOne(FundFullInfo fundFullInfo) {
        return fundFullInfoDao.addOne(fundFullInfo);
    }

    @Override
    public int addMulti(List<FundFullInfo> fundFullInfos) {
        return fundFullInfoDao.addMulti(fundFullInfos);
    }

    @Override
    public FundFullInfo selectLocal(String fundCode) {
        return fundFullInfoDao.selectLocal(fundCode);
    }

    @Override
    public int updateOne(FundFullInfo fundFullInfo) {
        return fundFullInfoDao.updateOne(fundFullInfo);
    }

    @Override
    public int delete(List<Integer> id) {
        return fundFullInfoDao.delete(id);
    }

    @Override
    public List<FundFullInfo> selectBYPolicy(String policy) {
        return fundFullInfoDao.selectBYPolicy(policy);
    }

    @Override
    public List<FundFullInfo> selectAll() {
        return fundFullInfoDao.selectAll();
    }

    @Override
    public  List<FundFullInfo> selectKey(String select) {
        return fundFullInfoDao.selectKey(select);
    }

    @Override
    public FundMsg selectOne(int id) {
        //获取基金信息
        FundFullInfo fundFullInfo = fundFullInfoDao.selectOne(id);
        //获取基金经理信息
        List<FundManagers> fundManagers = fundManagersDao.selectOne(fundFullInfo.getFundCode());
        //获取基金评级机构信息
        List<FundGrades> fundGrades = fundGradesDao.selectOne(fundFullInfo.getFundCode());
        //获取基金持仓信息
        FundPosition fundPosition = fundPositionDao.selectLocal(fundFullInfo.getFundCode());
        //new一个返回对象
        FundMsg fundMsg = new FundMsg();
        fundMsg.setFundFullInfo(fundFullInfo);
        fundMsg.setFundManagers(fundManagers);
        fundMsg.setFundPosition(fundPosition);
        fundMsg.setFundGrades(fundGrades);
        log.info("对象信息{}", fundMsg);
        return fundMsg;
    }

    @Override
    public List<FundFullInfo> selectDelete() {
        return fundFullInfoDao.selectDelete();
    }

    @Override
    public List<FundFullInfo> selectKeyDelete(String select) {
        return fundFullInfoDao.selectKeyDelete(select);
    }

    @Override
    public int deleteDel(List<Integer> id) {
        return fundFullInfoDao.deleteDel(id);
    }

    @Override
    public int recoverFund(int id) {
        return fundFullInfoDao.recoverFund(id);
    }

    @Override
    public List<Integer> selectAllCode() {
        return fundFullInfoDao.selectAllCode();
    }

    @Override
    public void updateAll(FundFullInfo newInfos) {
        fundFullInfoDao.updateAll(newInfos);
    }

    @Override
    public List<FundNavigation> showNavigation() {
        return fundNavigationDao.selectAll();
    }

    @Override
    public ResultBean selectNet(String fundCode) {
        String appcode = appCodeDao.selectById("fund");
        HttpHeaders httpHeaders = new HttpHeaders();
        //定义传输的数据的数据方式
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.set(HttpHeaders.AUTHORIZATION, "APPCODE " + appcode);
        //组装数据
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, httpHeaders);
        //外部接口的地址
        String url = "https://fund.market.alicloudapi.com/getFundDetail?fundcode=" + fundCode;
        ResponseEntity<FundMsg> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, FundMsg.class);
        FundMsg fundMsg = exchange.getBody();
        //取出基金详细信息
        FundFullInfo fundFullInfo = fundMsg.getFundFullInfo();
        //插入基金详细信息
        addOne(fundFullInfo);

        //取出基金评级机构信息
        List<FundGrades> fundGrades = fundMsg.getFundGrades();
        if (fundGrades.size() != 0) {
            //设置评级机构基金code
            for (FundGrades f : fundGrades) {
                f.setFundCode(fundFullInfo.getFundCode());
            }
            //插入基金评级机构
            fundGradesDao.addOne(fundGrades);
        }
        //取出基金历史基金经理
        List<FundManagers> fundManagers = fundMsg.getFundManagers();
        if (fundManagers.size() != 0) {
            //设置基金经理基金code
            for (FundManagers f : fundManagers) {
                f.setFundCode(fundFullInfo.getFundCode());
            }
            //插入基金历史基金经理
            fundManagersDao.addOne(fundManagers);
        }
        //在线查找基金持仓并存进数据库
        fundPositionService.selectAdd(fundFullInfo.getFundCode());
        return ResultBean.builder().code(200).msg("新增成功").build();
    }

    @Override
    public FundFullInfo selectById(int id) {
        return fundFullInfoDao.selectById(id);
    }
}